package com.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.model.Company;
import com.service.CompanyService;

@Controller
@RequestMapping("/company")
public class CompanyController{
	@Autowired
	CompanyService companyService;
	/**
	 * 更改密码
	 * @param model
	 * @param mail
	 * @param oldPassword
	 * @param newPassword1
	 * @param newPassword2
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updatePassword",method={RequestMethod.POST,RequestMethod.GET})
	public String updatePassword(Model model,String oldPassword,String newPassword1,String newPassword2,HttpSession session) throws Exception {
		try{
			String mail=(String) session.getAttribute("mail");
			Company company=companyService.findCompanyByMail(mail);
			if(company.getPassword().equals(oldPassword)){
				if(newPassword1.isEmpty()||newPassword2.isEmpty()){
					//旧密码为空
					model.addAttribute("error", "newpassword is empty");
					return "boos_updatepwd_com";
				}
				else if(newPassword1.equals(newPassword2)){
					//更改密码成功
					company.setPassword(newPassword1);
					companyService.updatePassword(company);
					return "boos_pwdupdate_success";
				}else{
					//密码1和密码2不相同
					model.addAttribute("error", "password1 isnot equal with password2");
					return "boos_updatepwd_com";
				}
			}else{
				//旧密码不正确
				model.addAttribute("error", "password incorrect");
				return "boos_updatepwd_com";
			}
		}catch(Exception e){
			//账号不存在
			model.addAttribute("error", "the username is not existed");
			return "boos_updatepwd_com";
		}
	}

}
